package com.seed.application.service;

import cn.hutool.core.util.StrUtil;
import com.seed.application.service.detail.DetailService;
import com.seed.application.service.get.GetService;
import com.seed.application.service.page.PageService;
import com.seed.application.service.remove.RemoveService;
import com.seed.application.service.save.SaveService;
import com.seed.application.service.set.SetService;
import com.seed.application.service.stat.StatService;
import com.seed.application.service.update.UpdateService;
import com.seed.core.RequestWrapper;
import com.seed.core.ResponseWrapper;
import com.seed.core.action.*;
import com.seed.core.pojo.seed.Seed;

public class SeedService1 {
    private final Seed seed;
    private final Action action;
    private final RequestWrapper request;
    private final ResponseWrapper response;

    public SeedService1(Seed seed, Action action, RequestWrapper request, ResponseWrapper response) {
        this.seed = seed;
        this.action = action;
        this.request = request;
        this.response = response;
    }

    public void exec() {
        response.sendJSON(getResult());
    }

    private Object getResult() {
        if(action instanceof PageAction pageAction) {
            Integer pageNumber = request.getInt("pageNumber", PageService.PAGE_NUMBER);
            Integer pageSize = request.getInt("pageSize", PageService.PAGE_SIZE);

            return new PageService(seed, pageAction)
                    .pageNumber(pageNumber)
                    .pageSize(pageSize)
                    .searchParams(request.getSearch())
                    .orderParams(request.getOrder())
                    .keyword(request.getKeyword())
                    .dataPermis(request.getDataPermis())
                    .fieldPermits(request.getFieldPermis())
                    .exec();
        }

        if(action instanceof SaveBatchAction saveBatchAction) {
            return new SaveService(seed, saveBatchAction).execBatch(request.getRows());
        }

        if(action instanceof SaveAction saveAction) {
            return new SaveService(seed, saveAction).exec(request.getRow());
        }

        if(action instanceof UpdateBatchAction updateBatchAction) {
            return new UpdateService(seed, updateBatchAction).execBatch(request.getRows());
        }

        if(action instanceof UpdateAction updateAction) {
            return new UpdateService(seed, updateAction).exec(request.getRow());
        }

        if(action instanceof DetailAction detailAction) {
            String id = request.get("id");
            if(StrUtil.isBlank(id)) throw new RuntimeException("参数不能为空");

            return new DetailService(seed, detailAction)
                    .dataPermis(request.getDataPermis())
                    .fieldPermits(request.getFieldPermis())
                    .exec(id);
        }

        if(action instanceof RemoveBatchAction removeBatchAction) {
            return new RemoveService(seed, removeBatchAction).execBatch(request.getIds());
        }

        if(action instanceof RemoveAction removeAction) {
            String id = request.get("id");
            if(StrUtil.isBlank(id)) throw new RuntimeException("参数不能为空");

            return new RemoveService(seed, removeAction).exec(id);
        }

        if(action instanceof GetBatchAction getBatchAction) {
            return new GetService(seed, getBatchAction).execBatch(request.getIds());
        }

        if(action instanceof GetAction getAction) {
            String id = request.get("id");
            if(StrUtil.isBlank(id)) throw new RuntimeException("参数不能为空");

            return new GetService(seed, getAction).exec(id);
        }

        if(action instanceof SetBatchAction setBatchAction) {
            return new SetService(seed, setBatchAction).execBatch(request.getSetDTOs());
        }

        if(action instanceof SetAction setAction) {
            return new SetService(seed, setAction).exec(request.getSetDTO());
        }

        if(action instanceof StatAction statAction) {
            return new StatService(seed, statAction)
                    .searchParams(request.getSearch())
                    .dataPermis(request.getDataPermis())
                    .keyword(request.getKeyword())
                    .exec();
        }

        throw new RuntimeException("未完待续");
    }
}
